﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Resources;
using System.Reflection;

namespace HomeDmsUtils
{
    public static class Localizer
    {
        public static string GetStr(string text, params object[] textParams)
        {
            //ResourceManager rm = new ResourceManager(
     //"HomeDmsUtils.Resource", Assembly.GetCallingAssembly());

            string locText = Properties.Resource.ResourceManager.GetString(CalcKey(text));
            if (string.IsNullOrEmpty(locText))
                locText = text;
            return string.Format(locText, textParams);
        }

        public static DialogResult MsgBox(IWin32Window owner, string text, string caption, 
            MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton, params object[] textParams)
        {
            if (Thread.CurrentThread.CurrentUICulture.TextInfo.IsRightToLeft)
                return MessageBox.Show(owner, GetStr(text, textParams), GetStr(caption), buttons, icon, defButton, MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign);
            else
                return MessageBox.Show(owner, GetStr(text, textParams), GetStr(caption), buttons, icon, defButton);
        }

        public static DialogResult MsgBox(IWin32Window owner, string text, string caption,
            MessageBoxButtons buttons, MessageBoxIcon icon, params object[] textParams)
        {
            return MsgBox(owner, text, caption, buttons, icon, MessageBoxDefaultButton.Button1, textParams);
        }

        public static string CalcKey(string t)
        {
            t = t.Replace("\n", "\\n");
            t = t.Replace("\r", "\\r");
            if (string.IsNullOrEmpty(t))
                return "";
            string key = "K";
            long s = 0;
            foreach (char ch in t)
            {
                s += (int)ch;
            }
            return key + s.ToString();
        }
    }
}
